package com.ll.studentservice.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ll.commonUtils.JwtUtils;
import com.ll.commonUtils.MD5;
import com.ll.commonUtils.R;
import com.ll.studentservice.entity.EduStudent;
import com.ll.studentservice.entity.EduTeacher;
import com.ll.studentservice.entity.vo.*;
import com.ll.studentservice.service.EduTeacherService;
import com.sun.xml.bind.v2.model.core.ID;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 教师 前端控制器
 * </p>
 *
 * @author ll
 * @since 2020-11-30
 */
@Api(description = "教师登录注册信息增删改查API") //swagger界面提示
@RestController
@RequestMapping("/studentservice/eduteacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    //查询单个教师
    @GetMapping("findOne/{id}")
    @ApiOperation(value = "查询单个教师(根据教师Id)") //swagger界面提示
    public R findOneStudent(@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        EduTeacherInfoVo eduTeacherInfoVo = new EduTeacherInfoVo();
        BeanUtils.copyProperties(eduTeacher, eduTeacherInfoVo);

        return R.ok().data("teacherInfo", eduTeacherInfoVo);
    }


    //查询所有教师
    @GetMapping("findAllTeacher")
    @ApiOperation(value = "查询所有教师") //swagger界面提示
    public R findAllTeacher(){
        List<EduTeacher> eduTeacherList = eduTeacherService.list(null);

        //过滤掉密码
        List<EduTeacherInfoVo> eduTeacherInfoVoList = new ArrayList<>();
        for (EduTeacher eduTeacher : eduTeacherList) {
            //创建没有密码属性的教师实体类
            EduTeacherInfoVo eduTeacherInfoVo = new EduTeacherInfoVo();

            //批量赋值
            BeanUtils.copyProperties(eduTeacher, eduTeacherInfoVo);

            //添加到没有密码属性的教师集合里
            eduTeacherInfoVoList.add(eduTeacherInfoVo);
        }

        //判断集合是否为空
        if (eduTeacherInfoVoList.size() == 0){
            return R.error().message("记录为空!");
        } else {
            return R.ok().data("allTeacherInfo", eduTeacherInfoVoList);
        }
    }

    //逻辑删除教师
    @DeleteMapping("{id}")
    @ApiOperation(value = "逻辑删除教师") //swagger界面提示
    public R deleteteacherById(@PathVariable String id){

        //因为逻辑不管是否删除成功都会返回true,所以先通过查询,判断是否有这个学生Id,
        //对象不为空证明有这个id,在进行逻辑删除, 否则返回错误
        EduTeacher eduTeacher = eduTeacherService.getById(id);

        if (StringUtils.isEmpty(eduTeacher)){
            return R.error().message("查询不到该教师信息,删除失败!");
        }

        //逻辑删除
        eduTeacherService.removeById(id);

        return R.ok().message("删除成功!");
    }

    /**
     * 条件查询,带分页 POST方式
     * 要在EduConfig配置文件中引入分页插件
     * Condition条件
     */
    @ApiOperation(value = "查询所有教师(带分页功能,带条件,POST方式)")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@ApiParam(name = "current", value = "当前页码", required = true)
                                      @PathVariable("current") long current,
                                  @ApiParam(name = "limit", value = "每页记录数", required = true)
                                  @PathVariable("limit") long limit,
                                  @ApiParam(name = "teacherQuery", value = "查询条件", required = false)
                                  @RequestBody(required = false) TeacherQuery teacherQuery){

        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);

        //创建查询条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        //多条件组合查询
        //mybatis学过 动态sql
        //判断条件值是否为空,如果不为空拼接条件
        String name = teacherQuery.getName();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        //判断条件值是否为空,如果不为空拼接条件
        if (!StringUtils.isEmpty(name)){
            //构建条件
            wrapper.like("name", name);
        }

        if (!StringUtils.isEmpty(begin)){
            //构建条件
            wrapper.ge("gmt_create", begin);
        }

        if (!StringUtils.isEmpty(end)){
            //构建条件
            wrapper.le("gmt_modified", end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        //调用方法的时候,底层封装,把分页所有数据封装到pageTeacher对象里面
        eduTeacherService.page(pageTeacher, wrapper);

        //总记录数
        long total = pageTeacher.getTotal();
        List<EduTeacher> eduTeacherList = pageTeacher.getRecords();

        //过滤掉密码之类的敏感信息
        List<EduTeacherInfoVo> eduTeacherInfoVoList = new ArrayList<>();
        for (EduTeacher eduTeacher : eduTeacherList) {
            EduTeacherInfoVo eduTeacherInfoVo = new EduTeacherInfoVo();
            BeanUtils.copyProperties(eduTeacher, eduTeacherInfoVo);
            eduTeacherInfoVoList.add(eduTeacherInfoVo);
        }

        return R.ok().data("total", total).data("teacherInfoList", eduTeacherInfoVoList);
    }

    //添加教师
    @ApiOperation("手动添加教师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody AddTeacher addTeacher){
        //加密密码
        addTeacher.setPassword(MD5.encrypt(addTeacher.getPassword()));

        EduTeacher eduTeacher = new EduTeacher();
        BeanUtils.copyProperties(addTeacher, eduTeacher);

        boolean save = eduTeacherService.save(eduTeacher);

        // 三元运算符
        return save ? R.ok().message("添加教师信息成功") : R.error().message("添加教师信息失败");

    }

    //教师注册
    @ApiOperation("教师注册")
    @PostMapping("registerTeacher")
    public R registerTeacher(@RequestBody RegisterVo registerVo){

        //教师注册
        boolean flag = eduTeacherService.register(registerVo);

        return flag ? R.ok().message("注册成功") : R.error().message("注册失败");
    }


    //教师登录
    @ApiOperation(value = "教师登录,传递手机号和密码,暂时不需要验证码") //swagger界面提示
    @PostMapping("login")
    public R loginUser(@RequestBody LoginVo loginVo){
        //loginVo对象里面封装手机号和密码
        //登录成功,返回token值,使用jwt生成
        String token = eduTeacherService.login(loginVo);

        return  R.ok().data("token", token);
    }

    //根据token获取用户信息
    @ApiOperation(value = "根据token获取用户信息(swagger里面不能测试,要在Postman里测试,参数:header中的key为token,value为登录后返回的token字符串)") //swagger界面提示
    @GetMapping("getTeacherInfo")
    public R getTeacherInfo(HttpServletRequest request){
        System.out.println("EduTeacherController ==> getTeacherInfo(根据token获取用户信息)");

        //调用jwt工具类的方法,根据request对象获取头信息,返回用户id
        String teacherId = JwtUtils.getMemberIdByJwtToken(request);

        //查询数据库,根据用户id查询用户数据
        EduTeacher eduTeacher = eduTeacherService.getById(teacherId);

        if (StringUtils.isEmpty(eduTeacher)){
            return R.error().message("用户未登陆");
        }

        //因为EduTeacher对象里有密码之类的的敏感信息,创建一个新的实体类(EduTeacherInfo)封装
        EduTeacherInfo eduTeacherInfo = new EduTeacherInfo();
        BeanUtils.copyProperties(eduTeacher, eduTeacherInfo);

        return R.ok().data("teacherInfo", eduTeacherInfo);
    }


}

